import dayjs from 'dayjs'
import { addVehicle, queryVehicleDetail, editVehicle } from '../../services/vehicle'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId:"",
    vehicleInfo:{},
    showUseCharacter:false,
    useCharacterColumns:['家用车辆','营运车辆'],
    currentUseCharacter:'',

    showIssueDate:false,
    showRegisterDate: false,
    issueCurrentDate: new Date().getTime(),
    registerCurrentDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("111")
    if(!options){
      return
    }
    const { currentId } = options
    console.log("currentId>>", currentId)
    this.setData({
      currentId: currentId || ""
    })
    if( currentId ){
      this.getVehicleDetail(currentId)
    }
  },

  onShow(){
    console.log("222")
  },

  async getVehicleDetail(id){
    let detailRes = await queryVehicleDetail(id)
    console.log("detailRes>>>", detailRes)
    if(detailRes.code === 200){
      this.setData({
        vehicleInfo: detailRes.data
      })
    }
  },

  // ===========onchange事件===============
  onChangePlateNo(event){
    this.setData({
      ['vehicleInfo.name']: event.detail,
      ['vehicleInfo.plateNo']: event.detail
    })
  },

  onChangeVehicleType(event){
    this.setData({
      ['vehicleInfo.vehicleType']: event.detail
    })
  },

  onChangeMileage(event){
    this.setData({
      ['vehicleInfo.mileage']: event.detail
    })
  },

  onChangeOwner(event){
    this.setData({
      ['vehicleInfo.owner']: event.detail
    })
  },

  onChangeEngineNo(event){
    this.setData({
      ['vehicleInfo.engineNo']: event.detail
    })
  },

  onChangeVin(event){
    this.setData({
      ['vehicleInfo.vin']: event.detail
    })
  },

  // ========使用性质Begin==========
  chooseUseCharacter(){
    console.log("触发使用性质的下拉按钮了")
    this.setData({
      showUseCharacter: true
    })
  },
  handleUseCharacter(){
    console.log("点击了输入区域")
    this.setData({
      showUseCharacter: true
    })
  },
  onChangeUseCharacter(info){
    this.setData({
      currentUseCharacter: info.detail.value
    })
  },
  onCancelUseCharacter(){
    this.setData({
      showUseCharacter: false,
      currentUseCharacter:''
    })
  },
  onConfirmUseCharacter(){
    this.setData({
      showUseCharacter: false,
      ['vehicleInfo.useCharacter']: this.data.currentUseCharacter
    })
  },
  // ============使用性质End====================

  chooseIssueDate(){
    this.setData({
      issueCurrentDate: this.data.vehicleInfo.issueDate || new Date().getTime(),
      showIssueDate: true
    })
  },

  handleIssueDate(){
    this.setData({
      showIssueDate: true
    })
  },

  onCancelIssueDate(){
    this.setData({
      showIssueDate: false
    })
  },

  onIssueDateInput(value){
    this.setData({
      issueCurrentDate: value.detail
    })
  },

  onConfirmIssueDate(value){
    const date = dayjs(value.detail).format('YYYY-MM-DD')
    this.setData({
      showIssueDate: false,
      ['vehicleInfo.issueDate']: date
    })
  },

  // ==================
  chooseRegisterDate(){
    this.setData({
      registerCurrentDate: this.data.vehicleInfo.registerDate || new Date().getTime(),
      showRegisterDate: true
    })
  },

  handleRegisterDate(){
    this.setData({
      showRegisterDate: true
    })
  },

  onCancelRegisterDate(){
    this.setData({
      showRegisterDate: false
    })
  },

  onRegisterDateInput(value){
    this.setData({
      registerCurrentDate: value.detail
    })
  },

  onConfirmRegisterDate(value){
    const date = dayjs(value.detail).format('YYYY-MM-DD')
    this.setData({
      showRegisterDate: false,
      ['vehicleInfo.registerDate']: date
    })
  },

  async confirmInfo(){
    console.log("vehicleInfo>>>", this.data.vehicleInfo)
    if(this.data.vehicleInfo.id){
      let editRes = await editVehicle(this.data.vehicleInfo)
      console.log("编辑res>>>", editRes)
      if(editRes.code === 200){
        wx.navigateBack()
      }
    }else{
      let addRes = await addVehicle(this.data.vehicleInfo)
      console.log("确认res>>>", addRes)
      if(addRes.code === 200){
        wx.navigateBack()
      }
    }


    
  },
})